package com.onion.codegen.gen;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onion.codegen.bean.JavaBeanProperties;
import com.onion.codegen.bean.GeneratorProperties;
import com.onion.codegen.utils.TemplateUtil;

public class MapperXmlGenerator extends BaseGenerator{

	@Override
	public void genJavaClasss(List<JavaBeanProperties> javaPropertiesList, GeneratorProperties javaEntityProperties)
			throws Exception {
		
		
		for (JavaBeanProperties item : javaPropertiesList) {
			StringBuffer resultMap = new StringBuffer();
			StringBuffer baseColumn = new StringBuffer();
			StringBuffer baseValueColumn = new StringBuffer();
			StringBuffer whereColumn = new StringBuffer();
			StringBuffer setColumn = new StringBuffer();
			
			
			List<String> columnNames = item.getColumnNames();
			List<String> dbcolumnNames = item.getDbcolumnNames();
			List<String> columnTypes = item.getColumnTypes();
			List<String> dbcolumnTypes = item.getDbcolumnTypes();
			int size = columnNames.size();
			for(int i=0;i<size;i++){
				if(columnNames.get(i).toUpperCase().equals("ID")) {
					resultMap.append("<id property=\"").append(columnNames.get(i)).append("\" column=\"").append(dbcolumnNames.get(i)).append("\" /> \n");
				}else {
					resultMap.append("		<result property=\"").append(columnNames.get(i)).append("\" column=\"").append(dbcolumnNames.get(i)).append("\" /> \n");
				}
				baseColumn.append(columnNames.get(i)).append(",");
				baseValueColumn.append("#{").append(columnNames.get(i)).append("},");
				String columnType = columnTypes.get(i);
				if(columnType!=null&&columnType.equals("String")) {
					whereColumn.append("		<if test=\"").append(columnNames.get(i)).append(" !=null and ").append(columnNames.get(i)).append("!= ''\">\n");
					setColumn.append("		<if test=\"").append(columnNames.get(i)).append(" !=null and ").append(columnNames.get(i)).append("!= ''\">\n");
				}else {
					whereColumn.append("		<if test=\"").append(columnNames.get(i)).append(" !=null \">\n");
					setColumn.append("		<if test=\"").append(columnNames.get(i)).append(" !=null \">\n");
				}
				whereColumn.append("			AND ").append(dbcolumnNames.get(i)).append("=").append("#{").append(columnNames.get(i)).append(",jdbcType=").append(dbcolumnTypes.get(i)).append("}\n");
				whereColumn.append("		</if>\n");
				
				setColumn.append("			AND ").append(dbcolumnNames.get(i)).append("=").append("#{").append(columnNames.get(i)).append(",jdbcType=").append(dbcolumnTypes.get(i)).append("},\n");
				setColumn.append("		</if>\n");
			}
			
			Map<String, Object> param = new HashMap<>();
			param.put("javaEntityName", item.getJavaName());
			param.put("resultMap",resultMap.toString() );
			param.put("baseColumn", baseColumn.deleteCharAt(baseColumn.length()-1));
			param.put("baseValueColumn", baseValueColumn.deleteCharAt(baseColumn.length()-1));
			param.put("whereColumn",whereColumn.deleteCharAt(baseColumn.length()-1));
			param.put("setColumn", setColumn.deleteCharAt(baseColumn.length()-1));
			param.put("tableName",item.getDbTable());
			
			StringBuffer path = new StringBuffer();
			path.append(javaEntityProperties.getBaseFoler()).append("/");
			path.append(javaEntityProperties.getPackageName().replace(".", "/"));
			path.append("/").append(item.getJavaName()).append("Mapper.xml");
			
			TemplateUtil.createTemplateFile(javaEntityProperties.isForce(),"mapperxml.ftl", path.toString(), param);
		}
	}
}
